package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.service.MenuService;
import com.itheima.service.PermissionService;
import com.itheima.service.RoleService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/createRole")
public class CreateRoleController {

    @Reference
   private MenuService menuService;
    @Reference
    private PermissionService permissionService;
    @Reference
    private RoleService roleService;

    //查询所有菜单
    @RequestMapping("/findAllMenu")
    public Result findAllMenu(){

        try {
            List<Menu> menus = menuService.findAllMenu();
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS,menus);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }
    }

    //查询所有权限
    @RequestMapping("/findAllPermission")
    public Result findAllPermission(){

        try {
            List<Permission> permissions = permissionService.findAllPermission();
            return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS,permissions);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }

    //添加角色
    @RequestMapping("/add")
    public Result add(Integer[] permissionIds, Integer[] checkitemIds, @RequestBody Role role){

        try {
            roleService.add(role,checkitemIds,permissionIds);
            return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ROLE_FAIL);
        }
    }

    //分页
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){

        PageResult pageResult = roleService.findPage(queryPageBean);

        return pageResult;

    }

    //回显数据
    @RequestMapping("/findById")
    public Result findById(Integer id){

        try {
            Role role = roleService.findById(id);
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS,role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }
    //回显数据菜单
    @RequestMapping("/findAllByMenuByRoleId")
    public Result findAllByMenuByRoleId(Integer id){

        try {
            List<Integer> list = menuService.findAllByMenu(id);
            return new Result(true, MessageConstant.GET_MENU_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MENU_FAIL);
        }
    }
    //回显数据权限
    @RequestMapping("/findAllByPermissionByRoleId")
    public Result findAllByPermissionByRoleId(Integer id){

        try {
            List<Integer> list = permissionService.findAllByPermission(id);
            return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }

    //编辑
    @RequestMapping("/edit")
    public Result edit(Integer[] permissionIds,Integer[] checkitemIds,@RequestBody Role role){
        try {
            roleService.edit(permissionIds,checkitemIds,role);
            return new Result(true, MessageConstant.EDIT_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_ROLE_FAIL);
        }
    }
    //删除
    @PreAuthorize("hasAuthority('ROLE_DELETE')")
    @RequestMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Integer id){
        try {
            roleService.delete(id);
            return new Result(true, MessageConstant.DELETE_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_ROLE_FAIL);
        }
    }

}
